#include "memberprofile.h"
#include "ui_memberprofile.h"
#include "inquiresql.h"
#include "personmember.h"
#include "modifymembercardpassword.h"
#include "modifymemberinformation.h"
#include "modifymemberover.h"
#include <QMessageBox>

#include <QDesktopWidget>

MemberProfile::MemberProfile(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MemberProfile)
{
    ui->setupUi(this);
    this->setLayout(ui->MainLayout);

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    ui->pushButtonInquire->hide();
    m_modifyMembercardPassword = new ModifyMembercardPassword(this);
    m_modifyMemberInformation = new ModifyMemberInformation(this);
    m_modifyMemberOver = new ModifyMemberOver(this);
    m_modifyMembercardPassword->hide();
    m_modifyMemberInformation->hide();
    m_modifyMemberOver->hide();

    QHeaderView* vheaderView = ui->tableWidgetMemberProfile->verticalHeader();
    vheaderView->setHidden(true);

//    setTableWidgetHeaderStyle();
    displayMemberProfile("all");

    connect(m_modifyMemberInformation, SIGNAL(updateMemberProfile()),
            this, SLOT(slotUpdateMemberProfile()));
    connect(m_modifyMemberOver, SIGNAL(updateMemberProfile()),
            this, SLOT(slotUpdateMemberProfile()));
}

MemberProfile::~MemberProfile()
{
    delete ui;
}

void MemberProfile::setTableWidgetHeaderStyle()
{
    QStringList headers;
    headers << tr("会员卡号") << tr("会员姓名") << tr("性别") << tr("等级") << tr("会员卡额") << tr("积分")
            << tr("折扣") << tr("消费总额") << tr("消费次数") << tr("办卡日期")
            << tr("联系电话") << tr("电子邮箱") << tr("联系地址") << tr("备注");

    ui->tableWidgetMemberProfile->setColumnCount(14);
    ui->tableWidgetMemberProfile->setHorizontalHeaderLabels(headers);

    for (int i = 0; i < 14; ++i)
    {
        ui->tableWidgetMemberProfile->horizontalHeaderItem(i)->setFont(QFont("Helvetica"));
        ui->tableWidgetMemberProfile->horizontalHeaderItem(i)->setTextColor(QColor(200,111,30));
    }
}

void MemberProfile::displayMemberProfile(const QString &inquireMember)
{
    m_currentTableWidgetList.clear();
    m_currentTableWidgetList = m_sql->InquireMemberProfile();
    ui->tableWidgetMemberProfile->clear();
//qDebug() << "into displayMemberProfile";
    setTableWidgetHeaderStyle();

    //判断需要显示的会员资料行数
    int RowCount = 0;
    if (inquireMember != "all")
    {
        for(int i = 0; i < m_currentTableWidgetList.count(); ++i)
        {
            if (!(m_currentTableWidgetList.at(i).get_personID().contains(inquireMember, Qt::CaseInsensitive)
               || m_currentTableWidgetList.at(i).get_personName().contains(inquireMember, Qt::CaseInsensitive)
               || m_currentTableWidgetList.at(i).get_personTel().contains(inquireMember, Qt::CaseInsensitive)))
            {
                continue;
            }
            else
            {
                RowCount++;
            }
        }
        ui->tableWidgetMemberProfile->setRowCount(RowCount);
    }
    else
    {
        ui->tableWidgetMemberProfile->setRowCount(m_currentTableWidgetList.count());
    }

    //显示会员资料
    RowCount = 0;
    for(int i = 0; i < m_currentTableWidgetList.count(); ++i)
    {
        if (inquireMember != "all")
        {
            if (!(m_currentTableWidgetList.at(i).get_personID().contains(inquireMember, Qt::CaseInsensitive)
               || m_currentTableWidgetList.at(i).get_personName().contains(inquireMember, Qt::CaseInsensitive)
               || m_currentTableWidgetList.at(i).get_personTel().contains(inquireMember, Qt::CaseInsensitive)))
            {
                continue;
            }
        }

        QTableWidgetItem *membercardID = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_personID());
        QTableWidgetItem *memberName = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_personName());
        QTableWidgetItem *gender = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_personGender());

//        QTableWidgetItem *memberLevel;
//        if (m_currentTableWidgetList.at(i).get_memberLevel().isEmpty())
//        {
//            memberLevel = new QTableWidgetItem("");
//        }
//        else{
//        memberLevel = new QTableWidgetItem(
//                    m_currentTableWidgetList.at(i).get_memberLevel());
//}
        QTableWidgetItem *memberLevel = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_memberLevel());

        QTableWidgetItem *membercardAmount = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_membercardAmount());
        QTableWidgetItem *integration = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_integration());
        QTableWidgetItem *discount = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_discount());
        QTableWidgetItem *totalSpending = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_totalSpending());
        QTableWidgetItem *consumeTimes = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_consumeTimes());
        QTableWidgetItem *handleDate = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_handleDate());
        QTableWidgetItem *tel = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_personTel());
        QTableWidgetItem *email = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_personEmail());
        QTableWidgetItem *address = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_personAdress());
        QTableWidgetItem *memberRemark = new QTableWidgetItem(
                    m_currentTableWidgetList.at(i).get_memberRemark());

        //设置供货商编号,联系人为居中对齐
        membercardID->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        memberName->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        gender->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        memberLevel->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        membercardAmount->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter );
        integration->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        discount->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter );
        totalSpending->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter );
        consumeTimes->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        handleDate->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        tel->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        email->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        address->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter );
        memberRemark->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter );

        //设置文字字体大小
        membercardID->setFont(QFont("DejaVu Sans", 11, 2, false));
        memberName->setFont(QFont("DejaVu Sans", 11, 2, false));
        gender->setFont(QFont("DejaVu Sans", 11, 2, false));
        memberLevel->setFont(QFont("DejaVu Sans", 11, 2, false));
        membercardAmount->setFont(QFont("DejaVu Sans", 11, 2, false));
        integration->setFont(QFont("DejaVu Sans", 11, 2, false));
        discount->setFont(QFont("DejaVu Sans", 11, 2, false));
        totalSpending->setFont(QFont("DejaVu Sans", 11, 2, false));
        consumeTimes->setFont(QFont("DejaVu Sans", 11, 2, false));
        handleDate->setFont(QFont("DejaVu Sans", 11, 2, false));
        tel->setFont(QFont("DejaVu Sans", 11, 2, false));
        email->setFont(QFont("DejaVu Sans", 11, 2, false));
        address->setFont(QFont("DejaVu Sans", 11, 2, false));
        memberRemark->setFont(QFont("DejaVu Sans", 11, 2, false));

        ui->tableWidgetMemberProfile->setItem(RowCount, 0, membercardID);
        ui->tableWidgetMemberProfile->setItem(RowCount, 1, memberName);
        ui->tableWidgetMemberProfile->setItem(RowCount, 2, gender);
        ui->tableWidgetMemberProfile->setItem(RowCount, 3, memberLevel);
        ui->tableWidgetMemberProfile->setItem(RowCount, 4, membercardAmount);
        ui->tableWidgetMemberProfile->setItem(RowCount, 5, integration);
        ui->tableWidgetMemberProfile->setItem(RowCount, 6, discount);
        ui->tableWidgetMemberProfile->setItem(RowCount, 7, totalSpending);
        ui->tableWidgetMemberProfile->setItem(RowCount, 8, consumeTimes);
        ui->tableWidgetMemberProfile->setItem(RowCount, 9, handleDate);
        ui->tableWidgetMemberProfile->setItem(RowCount, 10, tel);
        ui->tableWidgetMemberProfile->setItem(RowCount, 11, email);
        ui->tableWidgetMemberProfile->setItem(RowCount, 12, address);
        ui->tableWidgetMemberProfile->setItem(RowCount, 13, memberRemark);

        RowCount++;
    }

    ui->tableWidgetMemberProfile->setCurrentItem(
                ui->tableWidgetMemberProfile->item(RowCount - 1, 1));

    //设置与文字相匹配的单元格大小
    ui->tableWidgetMemberProfile->resizeColumnsToContents();
    ui->tableWidgetMemberProfile->resizeRowsToContents();
}

const QString MemberProfile::get_autoAddMemberCardNumber()
{
    int memberCardNumber = m_currentTableWidgetList.at(
                m_currentTableWidgetList.count() - 1)
            .get_personID().toInt() + 1;
    return QString::number(memberCardNumber).rightJustified(8, '0');
}

void MemberProfile::setTableWidgetColor()
{
    for (int i = 0; i < ui->tableWidgetMemberProfile->rowCount(); ++i)
    {
        if (i % 2 == 0)
        {
            for (int j = 0; j < ui->tableWidgetMemberProfile->columnCount(); ++j)
            {
                QTableWidgetItem *item = ui->tableWidgetMemberProfile->item(i,j);
                if (item)
                {
                    const QColor color = QColor(252,222,156);
                    item->setBackgroundColor(color);
                }
            }
        }
    }
}

void MemberProfile::on_lineEditCommodity_textChanged(const QString &inquireMember)
{
    displayMemberProfile(inquireMember);
}

void MemberProfile::on_pushButtonModifyPassword_clicked()
{
    m_modifyMembercardPassword->move((QApplication::desktop()->width() - m_modifyMembercardPassword->width())/2,
                     (QApplication::desktop()->height() - m_modifyMembercardPassword->height())/2);
    m_modifyMembercardPassword->setWindowFlags(Qt::Dialog);
    //在对 QWidget 操作时，不能操作其他东西
    m_modifyMembercardPassword->setWindowModality(Qt::ApplicationModal);
    m_modifyMembercardPassword->setAttribute(Qt::WA_ShowModal, true);
    m_modifyMembercardPassword->displayMembercardID(
                ui->tableWidgetMemberProfile->item(ui->tableWidgetMemberProfile->currentRow(), 0)->text());
    m_modifyMembercardPassword->show();
}

void MemberProfile::on_pushButtonModifyInformation_clicked()
{
    m_modifyMemberInformation->move((QApplication::desktop()->width() - m_modifyMemberInformation->width())/2,
                     (QApplication::desktop()->height() - m_modifyMemberInformation->height())/2);
    m_modifyMemberInformation->setWindowFlags(Qt::Dialog);
    //在对 QWidget 操作时，不能操作其他东西
    m_modifyMemberInformation->setWindowModality(Qt::ApplicationModal);
    m_modifyMemberInformation->setAttribute(Qt::WA_ShowModal, true);
    m_modifyMemberInformation->displayMemberInformation(
                ui->tableWidgetMemberProfile->item(ui->tableWidgetMemberProfile->currentRow(), 0)->text());
    m_modifyMemberInformation->show();
}

void MemberProfile::slotUpdateMemberProfile()
{
    displayMemberProfile("all");
}

void MemberProfile::on_pushButtonDelete_clicked()
{
    switch(QMessageBox::question(this, tr("(☆_☆)温馨提示 ..."),
                tr("\n您确定要删除《卡号为:%1 姓名为:%2》的会员信息吗?")
                .arg(ui->tableWidgetMemberProfile->item(ui->tableWidgetMemberProfile->currentRow(), 0)->text(),
                     ui->tableWidgetMemberProfile->item(ui->tableWidgetMemberProfile->currentRow(), 1)->text()),
                          QMessageBox::No | QMessageBox::Yes ,QMessageBox::Yes))
    {
        case QMessageBox::Yes:
        {
            m_sql->DeleteMemberInformation(
                        ui->tableWidgetMemberProfile->item(ui->tableWidgetMemberProfile->currentRow(), 0)->text());
            displayMemberProfile("all");
            break;
        }
        case QMessageBox::No:
            break;
        default:
            break;
    }
}

void MemberProfile::slot_updateMemberProfile()
{
//    emit updateMemberProfile();
    m_modifyMemberOver->UpdateMemberLevel();
}

void MemberProfile::on_pushButtonModifyMemberOver_clicked()
{
    m_modifyMemberOver->move((QApplication::desktop()->width() - m_modifyMemberOver->width())/2,
                     (QApplication::desktop()->height() - m_modifyMemberOver->height())/2);
    m_modifyMemberOver->setWindowFlags(Qt::Dialog);
    //在对 QWidget 操作时，不能操作其他东西
    m_modifyMemberOver->setWindowModality(Qt::ApplicationModal);
    m_modifyMemberOver->setAttribute(Qt::WA_ShowModal, true);
    m_modifyMemberOver->displayMemberOver(
                ui->tableWidgetMemberProfile->item(ui->tableWidgetMemberProfile->currentRow(), 0)->text());
    m_modifyMemberOver->show();
}
